package com.example.exceltosqlite.ui.sqldao.update;

import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.Toast;

import com.example.exceltosqlite.R;
import com.example.exceltosqlite.base.BaseActivity;
import com.example.exceltosqlite.bean.MyConstant;
import com.example.exceltosqlite.dao.ExcelFileDao;
import com.example.exceltosqlite.dao.ExcelFileNumberDao;
import com.example.exceltosqlite.dao.ExcelFileTypeDao;
import com.example.exceltosqlite.entity.SqliteFileEntity;
import com.example.exceltosqlite.entity.SqliteFileNumberEntity;
import com.example.exceltosqlite.entity.SqliteFileTypeEntity;
import com.example.exceltosqlite.ui.sqldao.SpinnerFileAdapter;

import org.greenrobot.eventbus.EventBus;

import java.sql.SQLException;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class SqlUpdateActivity extends BaseActivity {

    @BindView(R.id.typeET)
    EditText typeET;
    @BindView(R.id.numberET)
    EditText numberET;
    @BindView(R.id.nameET)
    EditText nameET;
    @BindView(R.id.sqliteLV)
    ListView sqliteLV;
    @BindView(R.id.typeBT)
    Button typeBT;
    @BindView(R.id.numberBT)
    Button numberBT;
    @BindView(R.id.nameBT)
    Button nameBT;
    @BindView(R.id.text00ET)
    EditText text00ET;
    @BindView(R.id.text01ET)
    EditText text01ET;
    @BindView(R.id.text02ET)
    EditText text02ET;
    @BindView(R.id.text03ET)
    EditText text03ET;
    @BindView(R.id.text04ET)
    EditText text04ET;
    @BindView(R.id.text05ET)
    EditText text05ET;
    @BindView(R.id.text06ET)
    EditText text06ET;
    @BindView(R.id.text07ET)
    EditText text07ET;
    @BindView(R.id.text08ET)
    EditText text08ET;
    @BindView(R.id.text09ET)
    EditText text09ET;
    @BindView(R.id.updateFileLL)
    LinearLayout updateFileLL;
    @BindView(R.id.updateFileBT)
    Button updateFileBT;
    @BindView(R.id.deleteBT)
    Button deleteBT;
    private SqliteFileTypeEntity intentSqliteTypeEntity;
    private SqliteFileNumberEntity intentSqliteNumberEntity;
    private List<SqliteFileEntity> intentSqliteFileLists;
    private SpinnerFileAdapter spinnerFileAdapter;
    private ExcelFileTypeDao excelFileTypeDao;
    private ExcelFileNumberDao excelFileNumberDao;
    private ExcelFileDao excelFileDao;
    private SqliteFileEntity curItem; //当前选中的item

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_sql_update);
        ButterKnife.bind(this);
        init();
    }

    private void init() {
        intentSqliteTypeEntity = (SqliteFileTypeEntity) getIntent().getSerializableExtra(MyConstant.STR_NAME_1);
        intentSqliteNumberEntity = (SqliteFileNumberEntity) getIntent().getSerializableExtra(MyConstant.STR_NAME_2);
        intentSqliteFileLists = (List<SqliteFileEntity>) getIntent().getSerializableExtra(MyConstant.STR_NAME_3);
        spinnerFileAdapter = new SpinnerFileAdapter(this);
        excelFileTypeDao = new ExcelFileTypeDao(this);
        excelFileNumberDao = new ExcelFileNumberDao(this);
        excelFileDao = new ExcelFileDao(this);
        typeET.setText(intentSqliteTypeEntity.getType());
        numberET.setText(intentSqliteNumberEntity.getMessageNo());
        nameET.setText(intentSqliteNumberEntity.getMessageName());
        sqliteLV.setAdapter(spinnerFileAdapter);
        sqliteLV.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                curItem = (SqliteFileEntity) parent.getItemAtPosition(position);
                addFileData();

            }
        });
        spinnerFileAdapter.setItems(intentSqliteFileLists);
    }


    private void addFileData() {
        sqliteLV.setVisibility(View.GONE);
        updateFileLL.setVisibility(View.VISIBLE);
        updateFileBT.setVisibility(View.VISIBLE);
        text00ET.setText(curItem.getCol00());
        text01ET.setText(curItem.getCol01());
        text02ET.setText(curItem.getCol02());
        text03ET.setText(curItem.getCol03());
        text04ET.setText(curItem.getCol04());
        text05ET.setText(curItem.getCol05());
        text06ET.setText(curItem.getCol06());
        text07ET.setText(curItem.getCol07());
        text08ET.setText(curItem.getCol08());
        text09ET.setText(curItem.getCol09());

    }

    @OnClick({R.id.typeBT, R.id.numberBT, R.id.nameBT, R.id.updateFileBT, R.id.deleteBT})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.typeBT:
                try {
                    List<SqliteFileTypeEntity> allType = excelFileTypeDao.queryAll();
                    intentSqliteTypeEntity.setType(typeET.getText().toString().trim());
                    if (allType.contains(intentSqliteTypeEntity)) {
                        Toast.makeText(this, "存在相同的类型名称，修改失败", Toast.LENGTH_SHORT).show();
                    } else {
                        excelFileTypeDao.update(intentSqliteTypeEntity);
                        Toast.makeText(this, "类型修改成功", Toast.LENGTH_SHORT).show();
                    }
                } catch (SQLException e) {
                    e.printStackTrace();
                }
                break;
            case R.id.numberBT:
                try {
                    List<SqliteFileNumberEntity> allNumber = excelFileNumberDao.queryAll();
                    intentSqliteNumberEntity.setMessageNo(numberET.getText().toString().trim());
                    if (allNumber.contains(intentSqliteNumberEntity)) {
                        Toast.makeText(this, "存在相同的类型编号，修改失败", Toast.LENGTH_SHORT).show();
                    } else {
                        excelFileNumberDao.update(intentSqliteNumberEntity);
                        Toast.makeText(this, "编号修改成功", Toast.LENGTH_SHORT).show();
                    }
                } catch (SQLException e) {
                    e.printStackTrace();
                }
                break;
            case R.id.nameBT:
                try {
                    intentSqliteNumberEntity.setMessageName(nameET.getText().toString().trim());
                    excelFileNumberDao.update(intentSqliteNumberEntity);
                    Toast.makeText(this, "名称修改成功", Toast.LENGTH_SHORT).show();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
                break;
            case R.id.updateFileBT:
                try {
                    curItem.setCol00(text00ET.getText().toString());
                    curItem.setCol01(text01ET.getText().toString());
                    curItem.setCol02(text02ET.getText().toString());
                    curItem.setCol03(text03ET.getText().toString());
                    curItem.setCol04(text04ET.getText().toString());
                    curItem.setCol05(text05ET.getText().toString());
                    curItem.setCol06(text06ET.getText().toString());
                    curItem.setCol07(text07ET.getText().toString());
                    curItem.setCol08(text08ET.getText().toString());
                    curItem.setCol09(text09ET.getText().toString());
                    excelFileDao.update(curItem);
                    spinnerFileAdapter.notifyDataSetChanged();
                    sqliteLV.setVisibility(View.VISIBLE);
                    updateFileLL.setVisibility(View.GONE);
                    updateFileBT.setVisibility(View.GONE);
                    cleanET();
                    Toast.makeText(this, "报词修改成功", Toast.LENGTH_SHORT).show();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
                break;

            case R.id.deleteBT:
                try {
                    //type表的id
                    String tempTypeID = intentSqliteNumberEntity.getTypeID()+"";
                    //删除所有报词
                    excelFileDao.deleteList(intentSqliteFileLists);
                    //删除number表的对应记录
                    excelFileNumberDao.delete(intentSqliteNumberEntity);
                    //查询出是否还存在与tempTypeID同样的number表的数据
                    List<SqliteFileNumberEntity> tempList = excelFileNumberDao.queryAll("id",tempTypeID);
                    if(null != tempList && 0 != tempList.size()){
                        //还存在  就不删除type表的对应数据
                    }else{
                        //不存在就删除type表的对应数据
                        excelFileTypeDao.delete(intentSqliteTypeEntity);
                    }
                    Toast.makeText(this, "删除成功", Toast.LENGTH_SHORT).show();
                    EventBus.getDefault().post(MyConstant.EVENT_MSG_3);
                    finish();
                } catch (SQLException e) {
                    Toast.makeText(this, "删除失败"+e.getMessage(), Toast.LENGTH_SHORT).show();
                    e.printStackTrace();
                }
                break;
        }
    }

    private void cleanET() {
        text00ET.setText("");
        text01ET.setText("");
        text02ET.setText("");
        text03ET.setText("");
        text04ET.setText("");
        text05ET.setText("");
        text06ET.setText("");
        text07ET.setText("");
        text08ET.setText("");
        text09ET.setText("");
    }
}
